package com.sdruixinggroup.mondayb2b.ui.fragments;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.google.gson.reflect.TypeToken;
import com.sdruixinggroup.mondayb2b.API.API;
import com.sdruixinggroup.mondayb2b.R;
import com.sdruixinggroup.mondayb2b.adapter.ChoosePayWayAdapter;
import com.sdruixinggroup.mondayb2b.adapter.QuickAdapter;
import com.sdruixinggroup.mondayb2b.interfaces.ObjectCallBack;
import com.sdruixinggroup.mondayb2b.models.Channels;
import com.sdruixinggroup.mondayb2b.ui.Activities.NongPayActivity;
import com.sdruixinggroup.mondayb2b.ui.Activities.OffLinePayActivity;
import com.sdruixinggroup.mondayb2b.utils.UserInfoUtil;
import com.zhy.http.okhttp.OkHttpUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import me.leefeng.lfrecyclerview.LFRecyclerView;
import okhttp3.Call;

/**
 * Created by 00029935 on 2017/11/19.
 */

public class ChoosePayWayFragment extends BaseFragment {


    @BindView(R.id.ib_back)
    ImageView ibBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.recyclerView)
    LFRecyclerView recyclerView;
    @BindView(R.id.loading)
    ProgressBar loading;
    @BindView(R.id.tv_right)
    TextView tvRight;

    private List<Channels.PaymentChannelsBean> data;
    private ChoosePayWayAdapter adapter;
    private Channels.PaymentChannelsBean selectData;
    private int pay_id;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        pay_id= getActivity().getIntent().getIntExtra("pay_id",-1);
    }

    @Override
    protected int getInflateLayout() {
        return R.layout.common_fragment_layout;
    }

    @Override
    protected void setUpView(View view) {
        tvTitle.setText("请选择支付方式");
        tvRight.setText("确定");
        tvRight.setVisibility(View.VISIBLE);
        data = new ArrayList<>();
        adapter = new ChoosePayWayAdapter(getContext());
        adapter.setData(data);
        recyclerView.setLoadMore(false);
        recyclerView.setRefresh(false);
        recyclerView.setLFRecyclerViewListener(new LFRecyclerView.LFRecyclerViewListener() {
            @Override
            public void onRefresh() {
                queryData();
            }

            @Override
            public void onLoadMore() {

            }
        });
        recyclerView.setAdapter(adapter);
        adapter.setOnItemClickListener(new QuickAdapter.OnItemClickListener<Channels.PaymentChannelsBean>() {
            @Override
            public void OnItemClick(Channels.PaymentChannelsBean data, int postion) {
                if (null != selectData) {
                    selectData.isChecked = false;
                }

                selectData = data;
                selectData.isChecked = true;
                adapter.notifyDataSetChanged();
                if (data.getPayment_channel_id() == 5) {  //"name": "线下打款"
                    Intent intent = getActivity().getIntent();
                    intent.setClass(getContext(),OffLinePayActivity.class);
                    startActivityForResult(intent,1);
                } else if (data.getPayment_channel_id() == 6) { //name": "农资贷"
                    Intent it = getActivity().getIntent();
                    it.setClass(getContext(),NongPayActivity.class);
                    startActivityForResult(it,1);
                }

            }
        });
        queryData();
    }


    @OnClick(R.id.ib_back)
    public void onClick() {

        getActivity().finish();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        ButterKnife.bind(this, rootView);
        return rootView;
    }

    private void queryData() {
        if (isFirstIn) {
            loading.setVisibility(View.VISIBLE);
        } else {
            isFirstIn = false;
        }

        String url = API.PAYMENT + UserInfoUtil.getInfoToken(getContext());
        HashMap<String, String> heades = new HashMap<>();
        heades.put("Accept", "*/*");
        TypeToken<Channels> typeToken = new TypeToken<Channels>() {
        };
        OkHttpUtils.get().url(url).headers(heades).build().execute(new ObjectCallBack<Channels>(typeToken) {
            @Override
            public void onError(Call call, Exception e, int id) {
                if (null != loading) {
                    loading.setVisibility(View.GONE);
                }
                if (null != recyclerView) {
                    recyclerView.stopRefresh(false);
                }
            }

            @Override
            public void onResponse(Channels response, int id) {
                data.clear();
                if (null != response) {
                    if (response.getErr_code() == 0) {
                        List<Channels.PaymentChannelsBean> paymentChannelsBeen = response.getPayment_channels();
                        for(Channels.PaymentChannelsBean paymentChannelsBean: paymentChannelsBeen){
                            if(paymentChannelsBean.getPayment_channel_id() == pay_id ){
                                paymentChannelsBean.isChecked = true;
                                selectData = paymentChannelsBean;
                            }
                        }
                        data.addAll(paymentChannelsBeen);
                        adapter.reflashData(data);
                    } else {
                        showMsgToast(response.getErr_msg());
                    }
                }

                if (null != loading) {
                    loading.setVisibility(View.GONE);
                }
                if (null != recyclerView) {
                    recyclerView.stopRefresh(true);
                }
            }
        });
    }

    @OnClick(R.id.tv_right)
    public void onClickRight() {
        Intent intent = new Intent();
        intent.putExtra("payWay", selectData);
        getActivity().setResult(Activity.RESULT_OK, intent);
        getActivity().finish();
    }


    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(resultCode != Activity.RESULT_OK){
            return;
        }

        if(requestCode == 1){
           String value = data.getStringExtra("value");
            selectData.value = value;
            Log.e("HQQ"," value"+value);
        }
    }
}
